package com.szty.libarytest.mediaProjection.handWrith

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.SurfaceView
import android.view.View
import androidx.annotation.LongDef
import com.szty.libarytest.R
import kotlinx.android.synthetic.main.activity_screen_record.*
import kotlin.math.abs

/**
 *@author : created by SunJH
 *时间：2021/7/29 002917
 *作用：橡皮擦功能的测试类
 */
class EraserTest(context: Context, attributeSet: AttributeSet) :
    View(context, attributeSet) {
    private var preX: Float = 0.0f
    private var preY: Float = 0.0f
    private var mBufferBitmap: Bitmap? = null
    private var mBufferCanvas: Canvas? = null

    companion object {
        const val EDIT_MODE_PEN = 0x1L      //画笔模式
        const val EDIT_MODE_ERASER = 0x2L   //橡皮擦模式
    }

    @Retention(AnnotationRetention.SOURCE)
    @LongDef(EDIT_MODE_PEN, EDIT_MODE_ERASER)
    annotation class EditMode

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (mBufferCanvas == null) {
            mBufferBitmap =
                Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888)
            //canvas绘制的内容，将会在这个mBufferBitmap内
            mBufferCanvas = Canvas(mBufferBitmap!!)
            mBufferCanvas?.drawColor(Color.WHITE)
        }
    }

    //画笔
    @EditMode
    var mMode: Long = EDIT_MODE_PEN

    //想要绘制额内容先绘制到这个增加的canvas对应的bitmap上
    //写完后再把这个bitmap的ARGB信息一次提交给上下文的Canvas去绘制
    private var mPath = Path()
    private var mPaint = Paint()

    init {
        mPaint.style = Paint.Style.STROKE
        mPaint.color = Color.RED
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.strokeWidth = 2f
        //抗锯齿
        mPaint.isAntiAlias = true
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                //将起点移动到当前坐标
                mPath.moveTo(event.x, event.y)
                //记录上次触摸的坐标，
                preX = event.x
                preY = event.y
            }
            MotionEvent.ACTION_MOVE -> {
                val previousX = preX
                val previousY = preY

                val bx = abs(event.x - previousX)
                val by = abs(event.x - previousY)
                if(bx >=3 || by >= 3){
                    val cx = (event.x + previousX) / 2
                    val cy = (event.y + previousY) / 2

                    mPath.quadTo(previousX, previousY, cx, cy)
                    preX = event.x
                    preY = event.y
                }
            }
            MotionEvent.ACTION_UP -> {
                mBufferCanvas?.drawPath(mPath,mPaint)
                mPath.reset()
            }
        }
        //调用方法重新绘制，
        invalidate()
        return true
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawBitmap(mBufferBitmap!!, 0f, 0f, null)
        canvas?.drawPath(mPath,mPaint)
    }

    fun setMode(@EditMode mode: Long) {
        mMode = mode
        when (mode) {
            EDIT_MODE_PEN -> {
                mPaint.strokeWidth = 2f
                mPaint.color = Color.RED

                mPaint.xfermode = null
            }
            EDIT_MODE_ERASER -> {
                mPaint.strokeWidth = 20f
                mPaint.color = Color.WHITE
                mPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
            }
        }
    }

    /**
     * 清空画布
     */
    fun clear() {

        mBufferCanvas?.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
        invalidate()
    }
}